<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a>客房中心</a></el-breadcrumb-item>
      <el-breadcrumb-item><a>房间类型</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="padding-bottom: 30px">
      <el-input placeholder="请输入房间名称：" v-model="room.name" prefix-icon="el-icon-search"
                style="width: 300px; padding-right: 10px;float: left"></el-input>
      <el-select placeholder="请选择房间类型" style="width: 300px; padding-right: 10px;float: left " v-model="room.type">
        <el-option v-for="item in roomType"
                   :key="item.id"
                   lable="item.type"
                   :value="item.type"></el-option>
      </el-select>
      <el-button @click="searchByRoomName(page,pageSize)" type="primary" icon="el-icon-search" style="float:left;">搜索
      </el-button>
      <el-button @click="goBack()" type="primary" style="float:left;">返回</el-button>
    </div>
    <el-divider></el-divider>
    <el-card class="box-card">
      <el-table
          :data="tableData"
          border
          style="width: 100%;:">
        <el-table-column
            fixed
            prop="roomId"
            label="房间编号"
            width="120">
        </el-table-column>
        <el-table-column
            fixed
            prop="type"
            label="房间类型"
            width="150">
        </el-table-column>
        <el-table-column
            prop="roomName"
            label="房间名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="price"
            label="房间价格(元/天)"
            width="150">
        </el-table-column>
        <el-table-column
            prop="description"
            label="房间简介"
            width="200"
            :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column
            prop="url"
            label="房间图片"
            width="150">
          <template slot-scope="scope">
            <el-image :src="scope.row.url"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="是否可入住" width="150" align="center">
          <template slot-scope="scope">
            <el-switch
                @change="updateEnable(scope.row)"
                v-model="scope.row.enable"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#999"
                :disabled=true>

            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            width="250">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
            <el-button v-if="scope.row.enable=='1'" @click="openOrderConfirm(scope.row)">生成订单</el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-dialog title="编辑房间" :visible.sync="dialogFormVisible">
        <el-form :model="editForm">
          <el-form-item label="房间名称" :label-width="formLabelWidth">
            <el-input v-model="editForm.roomName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="房间类型" :label-width="formLabelWidth">
            <el-input v-model="editForm.type" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="房间简介" :label-width="formLabelWidth">
            <el-input v-model="editForm.description" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="房间价格" :label-width="formLabelWidth">
            <el-input v-model="editForm.price" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()">确 定</el-button>
        </div>
      </el-dialog>
      <div class="block">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            >
        </el-pagination>
      </div>
    </el-card>

  </div>
</template>

<script>
export default {
  data() {
    return {
      roomType: [{type: '单人房'}, {type: '双人房'}, {type: '总统套房'}, {type: '电竞房'}, {type: '大床房'}, {type: ''}],
      total: 1,
      page: 1,
      pageSize: 5,
      room: {name: '', type: '', roomId: '',price:''},
      currentDate: new Date(),
      currentPage: 1,
      tableData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      editForm: {
        roomName: '',
        type: '',
        description: '',
        price: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    openOrderConfirm(room) {
      this.$router.push({
        path: '/go',
        query: {
          roomId: room.roomId,
          price:room.price,
          type:room.type,
        }
      })
    },
    updateEnable(room) {
      let enableStatus = [
        {'url': '/disable', text: '禁用'},
        {'url': '/enable', text: '启用'}
      ];
      let url = 'http://localhost:9085/rooms/'
          + room.id + enableStatus[room.enable].url;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: enableStatus[room.enable].text + '房间成功！',
            type: 'success'
          });
          this.loadRoomList(this.page, this.pageSize);
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadRoomList(this.page, this.pageSize);
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },


    handleSizeChange(val) {
      this.pageSize = val;
      this.loadRoomList(this.page, this.pageSize)
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loadRoomList(this.page, this.pageSize)
      console.log(`当前页: ${val}`);
    },
    openEditDialog(room) {
      let url = 'http://localhost:9085/rooms/' + room.id;
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.editForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadRoomList(this.page, this.pageSize);
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9085/rooms/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改成功！',
            type: 'success'
          });
          this.loadRoomList(this.page, this.pageSize);
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.dialogFormVisible = false;
              this.loadRoomList(this.page, this.pageSize);
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(room) {
      this.$confirm('是否删除名为【' + room.name + '】的房间？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(room);
      }).catch(() => {
      });
    },
    handleDelete(room) {
      let url = 'http://localhost:9085/rooms/' + room.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除房间成功！',
            type: 'success'
          });
          this.loadRoomList(this.page, this.pageSize);
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadRoomList(this.page, this.pageSize);
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    searchByRoomName(page, pageSize) {
      let url;
      console.log(this.room.name)
      console.log(this.room.type)
      if (this.room.type == '' && this.room.name == '')
        this.loadRoomList(this.page, this.pageSize);
      if (this.room.type != '') {
        if (this.room.name == '') {
          url = "http://localhost:9085/rooms/selectByRoomType" + "?type=" + this.room.type;
          console.log(url)
        } else {
          url = "http://localhost:9085/rooms/selectByRoomNameAndRoomType" + "?roomName=" + this.room.name + "&type=" + this.room.type;
          console.log(url)
        }
      } else {
        if (this.room.name != '') {
          url = "http://localhost:9085/rooms/selectByRoomName" + "?roomName=" + this.room.name;
          console.log(url)
        }
      }
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url, {
            params: {
              page, pageSize
            }
          }).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        console.log(responseBody.data);
        this.tableData = responseBody.data.list;
        this.total = responseBody.data.total;
      });
    },
    goBack() {
      this.handleCurrentChange(1);
    },
    loadRoomList(page, pageSize) {
      let url = 'http://localhost:9085/rooms';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url, {
            params: {
              page,
              pageSize
            }
          }).then((response) => {
        let responseBody = response.data;
        console.log(responseBody)
        this.tableData = responseBody.data.list;
        this.total = responseBody.data.total;
      });
    }
  },
  mounted() {
    this.loadRoomList(this.page, this.pageSize);
  }


}
</script>


<style>
::-webkit-input-placeholder {
  color: hotpink;
}

::-moz-placeholder {
  color: #0096dc;
}

:-moz-placeholder {
  color: red;
}


</style>
